package com.rbs.controller;

import com.rbs.entity.Phone;
import com.rbs.service.BrandService;
import com.rbs.service.Impl.BrandServiceImpl;
import com.rbs.service.Impl.PhoneServiceImpl;
import com.rbs.service.PhoneService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet(urlPatterns = "/selectByPageController")
public class SelectByPageController extends HttpServlet {
    private PhoneService phoneService = new PhoneServiceImpl();
    private BrandService brandService = new BrandServiceImpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取页号
        String curPageStr = request.getParameter("curPage");
        int curPage = 1;
        if (curPageStr != null){
            curPage = Integer.parseInt(curPageStr);
        }

        //获取每页展示的数据
        int PageSize = 3;
        int start = (curPage-1)*PageSize;
        List<Phone> phoneList = phoneService.selectByPage(start, PageSize);
        request.setAttribute("phoneList",phoneList);
        request.setAttribute("curPage",curPage);
        //获取总条数
        int count = phoneService.selectCount();
        //获取总页数
        int totalPage = count%3==0?count/PageSize:count/PageSize+1;
        request.setAttribute("totalPage",totalPage);
        Map<Integer,String> map = brandService.selectAllBrand();
        request.setAttribute("map",map);
        request.getRequestDispatcher("/showAllPhone.jsp").forward(request,response);
    }
}
